import type {
  HealthTagListResult,
  ReportCompareListResult,
  RiskAssessmentListResult,
  RiskAssessmentSubmitResult
} from "@/type/report";
import { http } from "@/utils/http";
import type {
  PageResultDataTracerVO,
  PageResultReportSearchResultVO,
  ReportCompareCategoryVO,
  ReportCompareResultVO,
  ReportDetailVO,
  ReportQueryForm,
  ReportSearchConditionVO,
  RequestResponse,
  TagInfoVO
} from "./types";

export const reportApi = {
  // 获取搜索条件字典值
  queryReportDict: () => {
    return http.request<RequestResponse<ReportSearchConditionVO>>(
      "get",
      "/api/v1/hdl/app/report/search/condition"
    );
  },
  // 获取报告列表
  queryReportList: (data: ReportQueryForm) => {
    return http.request<RequestResponse<PageResultReportSearchResultVO>>(
      "post",
      "/api/v1/hdl/app/report/search",
      {
        data
      }
    );
  },
  // 获取影像资料列表
  queryImageList: data => {
    return http.request<any>("post", "/api/v1/hdl/app/report/image/query", {
      data
    });
  },
  // 获取上传文件的预签名URL
  presignedApi: data => {
    return http.request<any>(
      "post",
      "/api/v1/hdl/common/upload/presigned-url",
      {
        data
      }
    );
  },
  // 上传影像
  uploadImage: data => {
    return http.request<any>("post", "/api/v1/hdl/app/report/image/upload", {
      data
    });
  },

  // 上传影像胶片
  uploadImageFilm: data => {
    return http.request<any>(
      "post",
      "/api/v1/hdl/app/report/image/film/upload",
      {
        data
      }
    );
  },
  /**
   *
   * @param params
   * @reportType 1:报告解读,2:完整报告
   * @returns
   */
  queryReportDetail: (params: {
    reportId: string | number;
    reportType: number;
  }) => {
    return http.request<RequestResponse<ReportDetailVO>>(
      "get",
      "/api/v1/hdl/app/report/detail",
      {
        params
      }
    );
  },

  postReportReview: (data: ReportReviewSaveFrom) => {
    return http.request<RequestResponse<any>>(
      "post",
      "/api/v1/hdl/app/report/review",
      {
        data
      }
    );
  },
  // 健康建议-详情
  healthSuggestionApi: (params: any) => {
    return http.request<any>("get", "/api/v1/hdl/app/report/healthSuggestion", {
      params
    });
  },
  // 风险评估-保存
  riskAssessmentSave: (data: any) => {
    return http.request<any>(
      "post",
      "/api/v1/hdl/app/report/riskAssessment/save",
      {
        data
      }
    );
  },
  healthSuggestionSave: (data: any) => {
    return http.request<any>(
      "post",
      "/api/v1/hdl/app/report/healthSuggestion/save",
      {
        data
      }
    );
  },
  //报告对比选择列表
  queryCompareList: (params: any) => {
    return http.request<ReportCompareListResult>(
      "get",
      "/api/v1/hdl/app/report/compare/select",
      { params }
    );
  },
  //报告对比-检查项目分类
  queryCompareCategory: (params: any) => {
    return http.request<ReportCompareCategoryVO>(
      "get",
      "/api/v1/hdl/app/report/compare/category",
      { params }
    );
  },
  //报告对比-结果
  queryCompareResult: (data: any) => {
    return http.request<RequestResponse<ReportCompareResultVO[]>>(
      "post",
      "/api/v1/hdl/app/report/compare/result",
      { data }
    );
  },
  // 风险评估列表
  queryRiskAssessmentList: (params: any) => {
    return http.request<RiskAssessmentListResult>(
      "get",
      "/api/v1/hdl/app/report/riskAssessment",
      { params }
    );
  },
  // 风险评估提交或者保存草稿
  postRiskAssessmentSubmit: (data: any) => {
    return http.request<RiskAssessmentSubmitResult>(
      "post",
      "/api/v1/hdl/app/report/riskAssessment/save",
      {
        data
      }
    );
  },
  // 健康标签列表
  queryHealthTagList: (params: any) => {
    return http.request<HealthTagListResult>(
      "get",
      "/api/v1/hdl/app/report/healthTag",
      { params }
    );
  },
  // 健康标签提交或者保存草稿
  postHealthTagSubmit: (data: any) => {
    return http.request<RiskAssessmentSubmitResult>(
      "post",
      "/api/v1/hdl/app/report/healthTag/save",
      {
        data
      }
    );
  },

  getTracerInfo: (params: any) => {
    return http.request<PageResultDataTracerVO>(
      "get",
      "/api/v1/hdl/app/data/tracer/info",
      {
        params
      }
    );
  },

  tagSearch: data => {
    return http.request<RequestResponse<TagInfoVO>>(
      "post",
      "/api/v1/hdl/app/tag/search",
      {
        data
      }
    );
  },
  // /api/v1/hdl/app/report/upload
  reportUpload: (data: ReportUploadFrom) => {
    return http.request<RequestResponse<any>>(
      "post",
      "/api/v1/hdl/app/report/upload",
      {
        data
      }
    );
  },

  tagGetByType: params => {
    return http.request<PageResultDataTracerVO>(
      "get",
      "/api/v1/hdl/app/tag/getByType",
      {
        params
      }
    );
  },

  // /api/v1/hdl/app/report/page/analysis
  reportPageAnalysis: (params: { reportId: number }) => {
    return http.request<RequestResponse<ReportContentCalibrationVO[]>>(
      "get",
      "/api/v1/hdl/app/report/reportContentAdjustment",
      {
        params
      }
    );
  },
  // /api/v1/hdl/app/report/page/analysis/edit
  reportPageAnalysisEdit: (data: { id: number; content: string }) => {
    return http.request<RequestResponse<any>>(
      "post",
      "/api/v1/hdl/app/report /reportContentAdjustment/edit",
      { data }
    );
  },

  // /api/v1/hdl/app/report/page/analysis/batch/edit
  reportPageAnalysisBatchEdit: (data: {
    reportId: number;
    items: { id: number; content: string }[];
    readyToPublish: boolean;
  }) => {
    return http.request<RequestResponse<any>>(
      "post",
      "/api/v1/hdl/app/report/reportContentAdjustment/batch/edit",
      { data }
    );
  },
  // 精准对比-获取检查项目
  queryCompareItems: (params: { userId: number; reportIds: number[] }) => {
    return http.request<RequestResponse<CompareItemVO[]>>(
      "get",
      "/api/v1/hdl/app/report/compare/item",
      { params }
    );
  },
  // 精准对比-获取对比结果
  queryCompareItemResult: (data: {
    userId: number;
    itemId: number;
    startTime: string;
    endTime: string;
  }) => {
    return http.request<RequestResponse<CompareItemResultVO[]>>(
      "post",
      "/api/v1/hdl/app/report/compare/item/result",
      { data }
    );
  },
  // 查看用户最近一次报告
  queryUserLatestReport: (params: {
    reportUserId: number;
    reportType: number;
  }) => {
    return http.request<RequestResponse<any>>(
      "get",
      "/api/v1/hdl/app/report/userLatestRepost",
      {
        params
      }
    );
  },
  // 更新关注度
  updateAttention: (data: UpdateAttentionRequest) => {
    return http.request<RequestResponse<UpdateAttentionResponse>>(
      "post",
      "/api/v1/hdl/app/report/attention/update",
      {
        data
      }
    );
  }
};
/**
 * 更新关注度请求参数
 */
export interface UpdateAttentionRequest {
  /**
   * 关注度 (1-5)
   */
  value: number;
  /**
   * 数据类型
   * 1: 报告
   * 2: 用户档案
   * 3: 家庭档案
   * 4: 用户
   */
  dataType: 1 | 2 | 3 | 4;
  /**
   * 数据ID
   * type=1时，dataId为报告ID
   * type=2时，dataId为用户档案ID
   * type=3时，dataId为家庭ID
   * type=4时，dataId为用户ID
   */
  dataId: number;
}
/**
 * 更新关注度响应数据
 */
export interface UpdateAttentionResponse {
  /**
   * 操作结果
   */
  result: boolean;
  /**
   * 消息
   */
  message: string;
}

export interface ReportUploadFrom {
  /**
   * 是否为紧急处理
   * true: 标记紧急
   * false: 不标记
   */
  emergencyProcess: boolean;
  /**
   * 支持同批次,多文件上传
   * 1、多个PDF、
   * 2、多张图片（jpg、jpeg、png 格式）
   * 上传报告文件
   */
  reportFileList: ReportUploadDTO[];
  /**
   * 报告类型, search/condition 接口返回的 healthTypeList 中的 id
   */
  reportType: number;
  [property: string]: any;
}

/**
 * ReportUploadDTO
 */
export interface ReportUploadDTO {
  /**
   * 原文件名称
   */
  resourceName: string;
  /**
   * 上传报告的 reportObjectKey
   */
  resourceObjectKey: string;
}

/**
 * 报告内容校准 VO
 *
 * ReportContentCalibrationVO
 */
export interface ReportContentCalibrationVO {
  /**
   * 文件名
   */
  fileName?: string;
  /**
   * 报告页面解析结果
   */
  pages?: ReportPageAnalysisVO[];
  /**
   * 报告ID
   */
  reportId?: number;
}

/**
 * 报告页面解析结果VO
 *
 * ReportPageAnalysisVO
 */
export interface ReportPageAnalysisVO {
  /**
   * 页面内容（markdown格式）
   */
  content?: string;
  /**
   * 是否存在影像资料：0-否，1-是
   */
  hasMedicalImaging?: boolean;
  /**
   * 页面解析结果ID
   */
  id?: number;
  /**
   * 页面元数据JSON
   */
  meta?: string;
  /**
   * 页码
   */
  pageNo?: number;
  /**
   * 当前页面的 TOS 存储键
   */
  pageTosKey?: string;
  /**
   * 页面类型
   */
  pageType?: string;
  /**
   * 当前页面的预签名 URL
   */
  pageUrl?: string;
  /**
   * 报告ID
   */
  reportId?: number;
  /**
   * 处理状态：0-处理中，1-成功，2-失败
   */
  status?: number;
}

/**
 * 精准对比检查项目 VO
 *
 * CompareItemVO
 */
export interface CompareItemVO {
  /**
   * 检查项目ID
   */
  id: number;
  /**
   * 检查项目名称
   */
  name: string;
}

/**
 * 精准对比结果 VO
 *
 * CompareItemResultVO
 */
export interface CompareItemResultVO {
  /**
   * 指标库ID
   */
  indexLibraryId: number;
  /**
   * 分类ID
   */
  categoryId: number;
  /**
   * 分类名称
   */
  categoryName: string;
  /**
   * 检查值
   */
  value: string;
  /**
   * 单位
   */
  unit: string;
  /**
   * 单位名称
   */
  unitName: string;
  /**
   * 单位最小值
   */
  unitMin: string;
  /**
   * 单位最大值
   */
  unitMax: string;
  /**
   * 异常状态：0-正常，1-异常
   */
  abnormalStatus: number;
  /**
   * 图表样式：0-折线图，1-柱状图等
   */
  chartStyle: number;
  /**
   * 检查日期
   */
  inspectionDate: string;
  /**
   * 异常信息
   */
  abnormalInfo: string;
}

/**
 * ReportReviewSaveFrom
 */
export interface ReportReviewSaveFrom {
  /**
   * 健康建议-驳回原因
   */
  healthSuggestionRejectReason?: string;
  /**
   * 健康建议-审核状态,1:驳回,2:通过
   */
  healthSuggestionStatus?: number;
  /**
   * 报告ID
   */
  reportId: number;
  /**
   * 风险评估-驳回原因
   */
  riskAssessmentRejectReason?: string;
  /**
   * 风险评估-审核状态,1:驳回,2:通过
   */
  riskAssessmentStatus?: number;
}
